<!--
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 14:41:20
 * @LastEditTime: 2023-08-01 10:50:54
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description:   :gridOps="gridOptions" 
-->
<template>
  <sc-VxeTable
    ref="xGrid1"
    :gridEvts="gridEvents"
    :tablecode="'market_customer_storage_receive'"
    :url="this.$API.market.customerStorage.list"
    :params="{ is_sample: 0 }"
  >
  </sc-VxeTable>

  <save-dialog
    v-if="dialog.save"
    ref="saveDialog"
    @success="handleSuccess"
    @closed="handleClose"
  >
  </save-dialog>
  <after-save-dialog
    v-if="dialog.after"
    ref="afterSaveDialog"
    @success="handleSuccess"
    @closed="dialog.after = false"
  >
  </after-save-dialog>
  <SavePro
    v-if="dialog.savePro"
    ref="savePro"
    @success="handleSuccess"
    @closed="dialog.savePro = false"
  >
  </SavePro>
  <savedelivery
    v-if="dialog.saveDelivery"
    ref="saveDelivery"
    @success="handleSuccess"
    @closed="dialog.saveDelivery = false"
  >
  </savedelivery>
  <SaveStyle
    v-if="dialog.saveStyle"
    ref="saveStyle"
    @success="handleSuccess"
    @closed="dialog.saveStyle = false"
  >
  </SaveStyle>
  <SaveBarcode
    v-if="dialog.saveBarcode"
    ref="saveBarcode"
    @success="handleSuccess"
    @closed="dialog.saveBarcode = false"
  >
  </SaveBarcode>
  <ViewCraft
    v-if="dialog.viewCraft"
    ref="viewCraft"
    @success="handleSuccess"
    @closed="dialog.viewCraft = false"
  >
  </ViewCraft>
</template>
<script>
import SaveDialog from "./save";
import SavePro from "./savepro";
import AfterSaveDialog from "./aftersave";
import savedelivery from "./../../delivery/save";
import SaveStyle from "./savestyle";
import ViewCraft from "./viewcraft";
import SaveBarcode from "./savebarcode";


import { ElNotification } from "element-plus";
export default {
  components: {
    SaveDialog,
    AfterSaveDialog,
    SavePro,
    savedelivery,
    SaveStyle,
    ViewCraft,
    SaveBarcode
},
  name: "market_customer_storage_receive",
  data() {
    return {
      dialog: {
        save: false,
        after: false,
        savePro: false,
        saveDelivery: false,
        saveStyle: false,
        saveBarcode: false,
        viewCraft: false,
      },
      gridEvents: {
        cellDblclick: ({ row }) => {
          this.edit(row);
        },
        add: () => {
          this.add();
        },
        setStyle: ({ button }) => {
          this.dialog.saveStyle = true;
          this.$nextTick(() => {
            this.$refs.saveStyle.open().setData(button.selectRecords);
          });
        },
        setBarcode: ({ button }) => {
          this.dialog.saveBarcode = true;
          this.$nextTick(() => {
            this.$refs.saveBarcode.open().setData(button.selectRecords);
          });
        },
        viewCraft: ({ button }) => {
          this.dialog.viewCraft = true;
          this.$nextTick(() => {
            this.$refs.viewCraft.open().setData(button.selectRecords[0].batchno);
          });
        },
        setPro: ({ button }) => {
          let customer = null,
            errmes = [];

          let ids = button.selectRecords.map(v => {
            if (v.status_id == 0) {
              errmes.push(errmes.length + 1 + "、必须审核后才可以开卡！！");
            }
            if (customer == null) {
              customer = v.customer_id;
            } else if (customer != v.customer_id) {
              errmes.push(errmes.length + 1 + "、批量开卡必须选择统一客户！！");
            }
            if (v.material.pro_allow == 0) {
              errmes.push(
                errmes.length +
                  1 +
                  "、" +
                  v.material.name +
                  "不允许开卡！！如必须开卡，请在基础资料中设置！"
              );
            }
            return v.id;
          });
          if (ids == null) {
            errmes.push(errmes.length + 1 + "、必须选择明细！！！");
          }
          if (errmes.length > 0) {
            throw errmes.join(";");
          }
          this.dialog.savePro = true;
          this.$nextTick(() => {
            this.$refs.savePro.open().setData(ids);
          });
        },

        setDelivery: ({ button }) => {
          let customer = null,
            customer_data = null,
            errmes = [];

          let ids = button.selectRecords.map(v => {
            customer_data = v.customer;
            if (customer == null) {
              customer = v.customer_id;
            } else if (customer != v.customer_id) {
              errmes.push(errmes.length + 1 + "、批量选择发货必须选择统一客户！！");
            }
            return v.batchno;
          });
          if (ids == null) {
            errmes.push(errmes.length + 1 + "、必须选择明细！！！");
          }
          if (errmes.length > 0) {
            throw errmes.join(";");
          }
          this.dialog.saveDelivery = true;
          this.$nextTick(() => {
            this.$refs.saveDelivery.open().setCustomerReceive(ids, customer_data);
          });
        },
        edit: ({ button }) => {
          this.edit(button.selectRecords[0]);
        },
        addCom: ({ button }) => {
          this.addCom(button.selectRecords[0]);
        },
        // delete2: ({ button }) => {
        //   this.delete(button.selectRecords);
        // },
      },
    };
  },
  methods: {
    add: function () {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open().setFormData({ is_sample: 0 });
      });
    },
    handleClose: function (mode) {
      if (mode != "add") this.dialog.save = false;
    },
    addCom: function (selected) {
      this.dialog.after = true;
      if (selected.instorage_way_id != 2)
        return ElNotification({
          title: "生成失败！",
          message: "客户入库单才允许生成退仓单！！",
          type: "error",
        });
      this.$nextTick(() => {
        this.$refs.afterSaveDialog.open("edit").setData(selected);
      });
    },
    handleSuccess() {
      this.$refs.xGrid1.refresh();
    },
    edit(selected) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(selected);
      });
    },
    tipsOK(res) {
      if (res.code == 200) {
        ElNotification({
          title: "删除成功",
          message: "删除成功",
          type: "success",
        });
        this.handleSuccess();
      }
    },
  },
};
</script>
